package logic

import (
	"context"
	"errors"
	"short-video-manager/models/cache"
	"time"

	"short-video-manager/videorpc/internal/svc"
	"short-video-manager/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SmsCodeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSmsCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SmsCodeLogic {
	return &SmsCodeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SmsCodeLogic) SmsCode(in *videorpc.SmsCodeRequest) (*videorpc.SmsCodeResponse, error) {
	// todo: add your logic here and delete this line
	if in.PhoneNum == "" {
		return nil, errors.New("发送短信验证码失败")
	}
	Count, _ := cache.Rdb.Get(l.ctx, "smsCodes"+in.PhoneNum).Int64()
	if Count >= 1 {
		return nil, errors.New("你已经在一分钟内发送过一次了，请稍后发送")
	}
	code := 1234
	//smsCode, _ := pkg.SmsCode(in.PhoneNum, strconv.Itoa(code))
	//if smsCode.Code != 2 {
	//	return nil, errors.New(smsCode.Msg)
	//}
	cache.Rdb.Set(l.ctx, "smsCode"+in.PhoneNum, code, time.Minute*5)
	result, _ := cache.Rdb.Incr(l.ctx, "smsCodes"+in.PhoneNum).Result()
	if result == 1 {
		cache.Rdb.Expire(l.ctx, "smsCodes"+in.PhoneNum, time.Minute*1)
	}
	return &videorpc.SmsCodeResponse{}, nil
}
